package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.querybean.QueryBlacklistBean;
import com.caiyi.financial.nirvana.sms.entity.SmsBlacklist;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsBlacklistMapper extends BaseMapper<SmsBlacklist> {

    @SelectProvider(type = BlacklistSqlProvider.class, method = "selectBlacklistListByParams")
    List<SmsBlacklist> selectBlacklistListByParams(QueryBlacklistBean bean);

    @Select("select count(id) from sms_blacklist where phone = #{phone}")
    int selectBlacklist(String phone);

    class BlacklistSqlProvider {
        public String selectBlacklistListByParams(QueryBlacklistBean bean) {
            return new SQL() {
                {
                    SELECT("id,phone,create_time,update_time ");
                    FROM("sms_blacklist");
                    if (!StringUtils.isEmpty(bean.getPhone())) {
                        WHERE("phone LIKE concat('%',#{phone},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }

    }
}
